﻿using Newtonsoft.Json.Linq;
using SRL_Statistics.Models;
using SRL_Statistics.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace SRL_Statistics.Controllers
{
    public class ScriptController : Controller
    {
        private DBAccessor accessor = new DBAccessor();
        //
        // GET: /Script/Graph?scriptName=script&userID=3
        //Need to add option for time based graphs.  Dunno how I plan on that yet - mainly because I'm having
        //trouble picturing it in my head for what type of graph it would be.  Recommendations?
        public ActionResult Graph()
        {
            return View();
        }
        
        public ActionResult Add(string scriptName, string passwordText, List<string> variables)
        {
            //accessor.RegisterScript(scriptName, passwordText, variables);
            return View();
        }

        public ActionResult Table()
        {
            return View();
        }

        public ActionResult Commit()
        {
            StreamReader stream = new StreamReader(Request.InputStream);
            String json = stream.ReadToEnd();
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            SubmissionModel data = jsonSerializer.Deserialize<SubmissionModel>(json);
            accessor.Commit(data.UserID, data.ScriptName, data.CommitLength, data.Variables);
            return Content("Success");
        }

        public ActionResult GetJsonStyle()
        {
            Dictionary<String, long> dic = new Dictionary<string, long>();

            dic.Add("RCEXP", 5700);
            dic.Add("GP", 7500);
            dic.Add("var", 3);
            SubmissionModel model = new SubmissionModel()
            {
                ScriptName = "Script",
                UserID = 0000014,
                CommitLength = 240,
                Variables = dic
            };
            return Json(model, JsonRequestBehavior.AllowGet);
        }
    }
}
